program abcode
	args code abbind occupation industry classworker

gen `abbind'=0

* occupations 215, 222
foreach occup in 215 222 {
replace `abbind'=917 if `occupation'==`occup' & `industry'==917
replace `abbind'=927 if `occupation'==`occup' & `industry'==927
replace `abbind'=937 if `occupation'==`occup' & `industry'!=917 & `industry'!=927
}
* occupation 245
replace `abbind'=99  if (`occupation'==245 & `industry' > 66 &  `industry' < 78) 
replace `abbind'=299 if (`occupation'==245 & `industry' > 106 & `industry' < 260) 
replace `abbind'=399 if (`occupation'==245 & `industry' > 267 & `industry' < 399)
replace `abbind'=439 if (`occupation'==245 & `industry' > 406 & `industry' < 430)
replace `abbind'=499 if (`occupation'==245 & `industry' > 446 & `industry' < 480)
replace `abbind'=599 if (`occupation'==245 & `industry' > 506 & `industry' < 589)
replace `abbind'=607 if (`occupation'==245 & `industry' > 606 & `industry' < 609)
replace `abbind'=609 if (`occupation'==245 & `industry' > 608 & `industry' < 628)
replace `abbind'=628 if (`occupation'==245 & `industry' > 627 & `industry' < 639)
replace `abbind'=639 if (`occupation'==245 & `industry' > 638 & `industry' < 648) 
replace `abbind'=639 if (`occupation'==245 & `industry' == 649)
replace `abbind'=648 if (`occupation'==245 & `industry' == 648)
replace `abbind'=657 if (`occupation'==245 & `industry' > 656 & `industry' < 659) 
replace `abbind'=667 if (`occupation'==245 & `industry' > 666 & `industry' < 669) 
replace `abbind'=699 if (`occupation'==245 & `industry' > 668 & `industry' < 699) 
replace `abbind'=719 if (`occupation'==245 & `industry' > 706 & `industry' < 719) 
replace `abbind'=727 if (`occupation'==245 & `industry' > 726 & `industry' < 760) 
replace `abbind'=799 if (`occupation'==245 & `industry' > 768 & `industry' < 799)
replace `abbind'=998 if (`occupation'==245 & `industry' > 16 & `industry' < 58)
replace `abbind'=998 if (`occupation'==245 & `industry' > 806 & `industry' < 938)
replace `abbind'=998 if (`occupation'==245 & `industry' ==999)
* occupations 394,395
foreach occup in 394 395 {
replace `abbind'=398 if (`industry' > 106 & `industry' < 399 & `occupation'==`occup')
replace `abbind'=498 if (`industry' > 406 & `industry' < 480 & `occupation'==`occup')
replace `abbind'=598 if (`industry' > 506 & `industry' < 699 & `occupation'==`occup')
replace `abbind'=719 if (`industry' > 706 & `industry' < 719 & `occupation'==`occup')
replace `abbind'=898 if (`industry' > 827 & `industry' < 898 & `occupation'==`occup')
replace `abbind'=939 if (`industry' > 906 & `industry' < 938 & `occupation'==`occup')
replace `abbind'=998 if (`industry' > 16 & `industry' < 78 & `occupation'==`occup')
replace `abbind'=998 if (`industry' > 726 & `industry' < 810 & `occupation'==`occup')
replace `abbind'=998 if (`industry' == 999 & `occupation'==`occup')
}
* occupation 441
replace `abbind'=99  if (`industry' > 66  & `industry' < 78 & `occupation'==441)
replace `abbind'=139 if (`industry' > 138 & `industry' < 170 & `occupation'==441)
replace `abbind'=177 if (`industry' > 176 & `industry' < 199 & `occupation'==441)
replace `abbind'=199 if (`industry' > 200 & `industry' < 210 & `occupation'==441)
replace `abbind'=219 if (`industry' > 218 & `industry' < 239 & `occupation'==441)
replace `abbind'=259 if (`industry' > 238 & `industry' < 260 & `occupation'==441)
replace `abbind'=268 if (`industry' > 267 & `industry' < 299 & `occupation'==441)
replace `abbind'=307 if (`industry' > 306 & `industry' < 328 & `occupation'==441)
replace `abbind'=398 if `occupation'==441 & (`industry'==299 | (`industry' > 327 & `industry' < 399))
replace `abbind'=439 if (`industry' > 406 & `industry' < 430 & `occupation'==441)
replace `abbind'=499 if (`industry' > 446 & `industry' < 480 & `occupation'==441)
replace `abbind'=598 if (`industry' > 506 & `industry' < 699 & `occupation'==441)
replace `abbind'=998 if (`occupation'==441 & `industry' > 16 & `industry' < 58)
replace `abbind'=998 if (`occupation'==441 & `industry' > 106 & `industry' <139)
replace `abbind'=998 if (`occupation'==441 & `industry' > 706 & `industry' <938)
replace `abbind'=998 if (`industry' == 999 & `occupation'==441)
* occupation 452
replace `abbind'=99  if (`occupation'==452 & `industry' > 66 &  `industry' < 78) 
replace `abbind'=407  if (`occupation'==452 & `industry'==407) 
replace `abbind'=999 if (`occupation'==452 & `industry' > 16 &  `industry' < 58) 
replace `abbind'=999 if (`occupation'==452 & `industry' > 106 & `industry' < 399)
replace `abbind'=999 if (`occupation'==452 & `industry' > 407 & `industry' < 938)
replace `abbind'=999 if (`occupation'==452 & `industry'==999)
* occupation 640
replace `abbind'=48  if (`occupation'==640 & `industry'==48)
replace `abbind'=49  if (`occupation'==640 & `industry'==49)
replace `abbind'=57  if (`occupation'==640 & (`industry'==47 | `industry'==57 | `industry'==999))
* occupations 690 692 694 695
foreach occup in 690 692 694 695 {
foreach single in 118 119 127 139 147 157 158 178 179 187 199 207 208 209 219 227 258 259 268 269 ///
278 287 289 299 319 327 328 329 337 338 339 347 349 358 379 387 388 389 397 398 407 999 {
replace `abbind'=`single' if (`occupation'==`occup' & `industry' == `single')
}
replace `abbind'=99  if (`occupation'==`occup' & `industry' > 66 & `industry' < 78)
replace `abbind'=107 if (`occupation'==`occup' & `industry' > 106 & `industry' < 110)
replace `abbind'=128 if (`occupation'==`occup' & `industry' > 127 & `industry' < 139)
replace `abbind'=148 if (`occupation'==`occup' & `industry' > 147 & `industry' < 150)
replace `abbind'=159 if (`occupation'==`occup' & `industry' > 158 & `industry' < 168)
replace `abbind'=168 if (`occupation'==`occup' & `industry' > 167 & `industry' < 170)
replace `abbind'=188 if (`occupation'==`occup' & `industry' > 187 & `industry' < 190)
replace `abbind'=197 if (`occupation'==`occup' & (`industry' == 197 | `industry' == 198 | `industry'==177))
replace `abbind'=228 if (`occupation'==`occup' & `industry' > 227 & `industry' < 239)
replace `abbind'=239 if (`occupation'==`occup' & `industry' > 238 & `industry' < 248)
replace `abbind'=239 if (`occupation'==`occup' & `industry'==257)
replace `abbind'=257 if (`occupation'==`occup' & `industry' >247 & `industry' < 250)
replace `abbind'=298 if (`occupation'==`occup' & (`industry'==279 | `industry'==288 | `industry'==297| `industry'==298))
replace `abbind'=369 if (`occupation'==`occup' & (`industry'==348 | `industry'==357))
replace `abbind'=369 if (`occupation'==`occup' & `industry' > 358 & `industry' < 370)
replace `abbind'=377 if (`occupation'==`occup' & (`industry'==377 | `industry'==378))
replace `abbind'=408 if (`occupation'==`occup' & `industry' > 407 & `industry' < 430)
replace `abbind'=499 if (`occupation'==`occup' & `industry' > 446 & `industry' < 480)
replace `abbind'=599 if (`occupation'==`occup' & `industry' > 506 & `industry' < 589)
replace `abbind'=600 if (`occupation'==`occup' & `industry' > 606 & `industry' < 699)
replace `abbind'=727 if (`occupation'==`occup' & `industry' > 726 & `industry' < 760)
replace `abbind'=939 if (`occupation'==`occup' & `industry' > 906 & `industry' < 938)
replace `abbind'=999 if (`occupation'==`occup' & `industry' > 16 & `industry' < 58)
replace `abbind'=999 if (`occupation'==`occup' & `industry' > 306 & `industry' < 319)
replace `abbind'=999 if (`occupation'==`occup' & `industry' > 706 & `industry' < 719)
replace `abbind'=999 if (`occupation'==`occup' & `industry' > 768 & `industry' < 898)
}
* occupations 780 785
foreach occup in 780 785 {
foreach single in 118 127 139 147 219 228 258 259 268 269 278 279 289 299 317 318 328 398 407 999 {
replace `abbind'=`single' if (`occupation'==`occup' & `industry' == `single')
}
replace `abbind'=107 if (`occupation'==`occup' & `industry' > 106 & `industry' < 110)
replace `abbind'=137 if (`occupation'==`occup' & `industry' ==128)
replace `abbind'=138 if (`occupation'==`occup' & (`industry' ==119 | `industry' ==137 | `industry' ==138))
replace `abbind'=148 if (`occupation'==`occup' & (`industry' ==149 | `industry' ==148))
replace `abbind'=169 if (`occupation'==`occup' & `industry' > 156 & `industry' < 170)
replace `abbind'=177 if (`occupation'==`occup' & `industry' > 176 & `industry' < 199)
replace `abbind'=199 if (`occupation'==`occup' & `industry' > 198 & `industry' < 210)
replace `abbind'=238 if (`occupation'==`occup' & `industry' > 228 & `industry' < 239)
replace `abbind'=238 if (`occupation'==`occup' & `industry' == 227)
replace `abbind'=239 if (`occupation'==`occup' & `industry' > 238 & `industry' < 258)
replace `abbind'=298 if (`occupation'==`occup' & `industry' > 286 & `industry' < 289)
replace `abbind'=298 if (`occupation'==`occup' & `industry' > 296 & `industry' < 299)
replace `abbind'=318 if (`occupation'==`occup' & `industry' > 306 & `industry' < 310)
replace `abbind'=319 if (`occupation'==`occup' & `industry' > 318 & `industry' < 328)
replace `abbind'=329 if (`occupation'==`occup' & `industry' > 328 & `industry' < 338)
replace `abbind'=338 if (`occupation'==`occup' & `industry' > 337 & `industry' < 340)
replace `abbind'=347 if (`occupation'==`occup' & `industry' > 346 & `industry' < 370)
replace `abbind'=377 if (`occupation'==`occup' & `industry' > 376 & `industry' < 379)
replace `abbind'=379 if (`occupation'==`occup' & `industry' > 378 & `industry' < 388)
replace `abbind'=388 if (`occupation'==`occup' & `industry' > 387 & `industry' < 398)
replace `abbind'=408 if (`occupation'==`occup' & `industry' > 407 & `industry' < 430)
replace `abbind'=499 if (`occupation'==`occup' & `industry' > 446 & `industry' < 480)
replace `abbind'=599 if (`occupation'==`occup' & `industry' > 506 & `industry' < 589)
replace `abbind'=600 if (`occupation'==`occup' & `industry' > 606 & `industry' < 699)
replace `abbind'=727 if (`occupation'==`occup' & `industry' > 726 & `industry' < 760)
replace `abbind'=799 if (`occupation'==`occup' & `industry' > 768 & `industry' < 799)
replace `abbind'=939 if (`occupation'==`occup' & `industry' > 906 & `industry' < 938)
replace `abbind'=999 if (`occupation'==`occup' & `industry' > 16 & `industry' < 78)
replace `abbind'=999 if (`occupation'==`occup' & `industry' > 706 & `industry' < 719)
replace `abbind'=999 if (`occupation'==`occup' & `industry' > 806 & `industry' < 898)
}
* lump occupations for the abcode
tempvar myoccup
clonevar `myoccup'=`occupation'
replace `myoccup'= 394 if `myoccup'==395
replace `myoccup'= 690 if `myoccup'==692 | `myoccup'==694 | `myoccup'==695
replace `myoccup'= 780 if `myoccup'==785


gen `code'=((`myoccup'*1000)+`abbind')*10
replace `code'=`code'+1 if  `myoccup'==245 & (`classworker'==3 | `classworker'==4)

end
